import os
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

def task():
    print(task.__module__, task.__name__)
    pid = os.getpid()
    print(f'This is task, pid is {pid}')
    time.sleep(1)


def create_thread_pool():
    with ThreadPoolExecutor(20) as executor:
        executor.submit(task)


def create_process_pool():
    with ProcessPoolExecutor(5) as executor:
        executor.submit(task)


def test1_process_create_thread_pool():
    # 通过进程去创建线程
    with ProcessPoolExecutor(5) as executor:
        executor.submit(create_thread_pool)


def test2_process_create_process_pool():
    # 通过进程，再次创建进程
    with ProcessPoolExecutor(5) as executor:
        executor.submit(create_process_pool)


def test3_thread_create_thread_pool():
    # 通过线程去创建线程
    with ThreadPoolExecutor(20) as executor:
        executor.submit(create_thread_pool)


def test4_thread_create_process_pool():
    # 通过线程再次创建进程
    with ThreadPoolExecutor(20) as executor:
        executor.submit(create_process_pool)


if __name__ == '__main__':
    test1_process_create_thread_pool()
    test2_process_create_process_pool()
    test3_thread_create_thread_pool()
    test4_thread_create_process_pool()